package mvp.com.bbc.rcjavaretrofitdaggermvp.app.utils;

import android.content.Context;
import android.widget.Toast;
import mvp.com.bbc.rcjavaretrofitdaggermvp.app.BaseApplication;


/**
 * 冒泡封装
 *
 * @author 轻吻旧时光
 * @date 2017年7月31日上午9:42:42
 */
public class ToastUtlis {
    public static void ToastShow(Context context, String content) {
        Toast.makeText(context, content, Toast.LENGTH_SHORT).show();
    }

    private static long lastClickTime;

    /**
     * 防止连续点击按钮
     *
     * @return
     */
    private synchronized static boolean isFastClick() {
        long time = System.currentTimeMillis();
        if (time - lastClickTime > 2000) {
            lastClickTime = time;
            return true;
        }
        return false;
    }

    private static Toast toast;

    /**
     * 冒泡——长
     *
     * @param context
     * @param content 冒泡内容
     */
    public static void ToastShow_Long(Context context, String content) {

        if (toast == null) {
            toast = Toast.makeText(BaseApplication.getContext(), content, Toast.LENGTH_LONG);
        } else {
            toast.setText(content);
        }
        toast.show();
    }

    /**
     * 冒泡——短
     *
     * @param context
     * @param content 冒泡内容
     */
    public static void ToastShow_Short(Context context, String content) {
        if (toast == null) {
            toast = Toast.makeText(BaseApplication.getContext(), content, Toast.LENGTH_SHORT);
        } else {
            toast.setText(content);
        }
        toast.show();
    }

    public static void cancelToast(){
        if (toast!=null){
            toast.cancel();
        }
    }
    /**
     * 自定义冒泡时间 单位：毫秒
     *
     * @param context
     * @param content  冒泡内容
     * @param duration 冒泡时间（单位：毫秒）
     */
    public static void show(Context context, String content, int duration) {
        if (isFastClick()) {
            Toast.makeText(context, content, duration).show();
        }
    }

}
